package cn.com.git.admin.dao.impl;

import cn.com.git.common.base.BaseDaoImpl;
import cn.com.git.admin.dao.SysMessageDAO;
import cn.com.git.admin.dao.mapper.SysMessageMapper;
import cn.com.git.admin.dao.model.SysMessage;
import cn.com.git.admin.dao.model.SysMessageExample;
import cn.com.git.admin.service.query.SysMessageListQuery;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
/**
  * SysMessageDAO接口
  * @author zhangzhenfu
  * @Description
  * @date 2020/5/18
*/
@Repository
public class SysMessageDAOImpl extends BaseDaoImpl<SysMessageMapper,SysMessage,SysMessageExample,SysMessageListQuery,Integer> implements SysMessageDAO {

    @Autowired
    private SysMessageMapper mapper;

    @Override
    protected SysMessageMapper getMapper() {
        return mapper;
    }

    @Override
    public SysMessageExample analysisLimit(SysMessageListQuery query) {
        SysMessageExample example = new SysMessageExample();
        SysMessageExample.Criteria c =  example.createCriteria();
        //TODO
        return example;
    }

    @Override
    public SysMessage selectById(Integer id) {
        return super.selectByPrimaryKey(id);
    }

    @Override
    public SysMessage selectOne(SysMessageListQuery query) {
        SysMessageExample example = analysisLimit(query);
        //TODO
        List<SysMessage> list =   Optional.ofNullable(super.selectByExample(example)).orElse(Lists.newArrayList());
            if(list.size()>0){
                return list.get(0);
            }
        return null;
    }

    @Override
    public List<SysMessage> selectList(SysMessageListQuery query) {
        SysMessageExample example = analysisLimit(query);
        return Optional.ofNullable(super.selectByExample(example)).orElse(Lists.newArrayList());
    }

    @Override
    public int countLimit(SysMessageListQuery query) {
        SysMessageExample example = analysisLimit(query);
        return countByExample(example);
    }

    @Override
    public Integer insertOne(SysMessage record) {
        super.insertSelective(record);
        return record.getId();
    }

    @Override
    public List<SysMessage> insertList(List<SysMessage> list) {
        Optional.ofNullable(list).orElse(Lists.newArrayList()).forEach(s->{
            super.insertSelective(s);
        });
        return list;
    }

    @Override
    public Integer updateOne(SysMessage record) {
        return super.updateByPrimaryKeySelective(record);
    }

    @Override
    public Integer updateLimit(SysMessage record, SysMessageListQuery query) {
        SysMessageExample example = analysisLimit(query);
        return super.updateByExampleSelective(record,example);
    }

    @Override
    public Integer deleteOne(Integer id) {
        return super.deleteByPrimaryKey(id);
    }

    @Override
    public Integer deleteLimit(SysMessageListQuery query) {
        SysMessageExample example = analysisLimit(query);
        return super.deleteByExample(example);
    }

}